package com.ruoyi.crm.strategy.impl;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.crm.strategy.ImageStorageStrategy;

/**
 * 阿里云OSS存储策略实现
 * 
 * @author ruoyi
 * @date 2025-11-03
 */
@Component
@ConditionalOnProperty(name = "image.storage.type", havingValue = "oss")
public class OssStorageStrategy implements ImageStorageStrategy {
    
    // OSS配置参数
    private String endpoint = "oss-cn-hangzhou.aliyuncs.com";
    private String accessKeyId = "your-access-key-id";
    private String accessKeySecret = "your-access-key-secret";
    private String bucketName = "your-bucket-name";
    private String domain = "https://your-domain.com";

    @Override
    public String uploadImage(MultipartFile file) throws Exception {
        // 这里实现阿里云OSS上传逻辑
        // 示例代码，实际使用时需要引入阿里云OSS SDK
        
        /*
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        
        // 文件名
        String fileName = "medicine/" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
        
        // 上传文件
        ossClient.putObject(bucketName, fileName, file.getInputStream());
        
        // 关闭OSSClient
        ossClient.shutdown();
        
        // 返回访问URL
        return domain + "/" + fileName;
        */
        
        // 示例返回
        return "https://example.com/medicine/sample.jpg";
    }

    @Override
    public void deleteImage(String imageUrl) throws Exception {
        // 实现删除逻辑
        // 示例代码
        /*
        String fileName = extractFileNameFromUrl(imageUrl);
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteObject(bucketName, fileName);
        ossClient.shutdown();
        */
    }

    @Override
    public String getStrategyName() {
        return "oss";
    }
    
    /**
     * 从URL中提取文件名
     * 
     * @param url URL
     * @return 文件名
     */
    private String extractFileNameFromUrl(String url) {
        // 实现提取逻辑
        return "sample.jpg";
    }
}